package com.ph.profit.api.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.base.BaseController;
import com.ph.shopping.common.util.excel.ExcelBean;
import com.ph.shopping.common.util.excel.ExcelUtil;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.verifycode.VerifyUtil;
import com.ph.shopping.facade.profit.entity.UnlineSupplyChainProfit;
import com.ph.shopping.facade.profit.request.ManagerProfitRequest;
import com.ph.shopping.facade.profit.service.IProfitService;
import com.ph.shopping.facade.profit.vo.UnlineManagerProfitVo;

/**
 * @项目：phshopping-api-platform
 * 
 * @描述：线下订单供应链分润相关
 * 
 * @作者： huangdd
 * 
 * @创建时间：2017年3月23日
 * 
 * @Copyright @2017 by huangdd
 */
@Controller
@RequestMapping("api/unLineOrderProfit")
public class UnLineOrderProfitController extends BaseController {

	@Reference(version = "1.0.0")
	private IProfitService iProfitService;

	/**
	 * 获取线下订单供应链利润分成列表(分页)
	 * 
	 * @return
	 */
	@RequestMapping(value = "/unLineOrderSupplyChainList", method = RequestMethod.GET)
	@ResponseBody
	public Object getUnlineOrderSupplyChainProfitVoList(
			@ModelAttribute UnlineSupplyChainProfit unlineSupplyChainProfit, PageBean pagebean) {
		return iProfitService.getUnlineOrderSupplyChainProfitVoList(unlineSupplyChainProfit,
				pagebean);
	}

	/**
	 * 跳转线下订单供应链利润分成列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/supplyChainProfitListPage", method = RequestMethod.GET)
	public String supplyChainProfitPage() {
		return "/profit/unlineOrderSupplyChainProfitList";
	}

	/**
	 * 导出线下订单供应链利润分成表EXCEL
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/exportUnlineOrderSupplyChainProfit", method = RequestMethod.GET)
	public Object exportUnlineOrderSupplyChainProfit(HttpServletRequest request,
			HttpServletResponse response) {
		Result supplyChainProfitList = iProfitService.exportUnlineOrderSupplyChainProfit();
		List<UnlineSupplyChainProfit> data = (List<UnlineSupplyChainProfit>) supplyChainProfitList
				.getData();
		List<Object[]> data2 = new ArrayList<Object[]>();
		for (UnlineSupplyChainProfit profit : data) {
			Object[] standard = new Object[23];
			standard[0] = profit.getOrderNo();
			standard[1] = profit.getPrice();
			standard[2] = profit.getCost();
			standard[3] = profit.getBuyExpense();
			standard[4] = profit.getLogisticsFee();
			standard[5] = profit.getChainProfit();
			standard[6] = profit.getPhIncome();
			standard[7] = profit.getYstIncome();
			standard[8] = profit.getShiName();
			standard[9] = profit.getCityProfit();
			standard[10] = profit.getXianName();
			standard[11] = profit.getCountyProfit();
			standard[12] = profit.getQuName();
			standard[13] = profit.getCommunityProfit();
			standard[14] = profit.getChainTotal();
			standard[15] = profit.getChainRemain();
			standard[16] = profit.getYstRemain();
			standard[17] = profit.getPhRemain();
			standard[18] = profit.getSellerName();
			standard[19] = profit.getBuyerName();
			standard[20] = profit.getWithdrawalsName();
			if (profit.getOrderType() == 1) {
				standard[21] = "分润";
			} else if (profit.getOrderType() == 2) {
				standard[21] = "商户提现";
			} else {
				standard[21] = "代理提现";
			}
			
			if (profit.getExamineType() == 1) {
				standard[22] = "未审核";
			} else if (profit.getExamineType() == 2) {
				standard[22] = "审核通过";
			}else {
				standard[22] = "审核不通过";
			}			
			data2.add(standard);
		}
		ExcelBean excelbean = new ExcelBean();
		excelbean.setTableHeader(new String[] { "订单号", "零售价", "结算成本", "门店进货价格", "物流费用", "供应链利润",
				"普惠供应链收入", "易商通供应链收入", "市代名称", "市级代理分润", "县代名称", "县级代理分润", "社区代名称", "社区代理分润",
				"供应链支出合计", "供应链剩余", "易商通总余", "普惠总余", "卖方名称", "买方名称", "提现人名称", "订单类型","审核状态"});
		excelbean.setSheetData(data2);
		excelbean.setName("线下订单供应链分润表");
		try {
			ExcelUtil.download(request, response, excelbean);
			// return getResult(ResultEnum.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			// return getResult(null);
		}
		return null;
	}

	/**
	 * 执行线下订单供应链利润分成(商户与代理商之间)
	 */
	// @RequestMapping(value = "/exportUnlineOrderSupplyChainProfit", method =
	// RequestMethod.GET)
	public void executiveUnLineOrderSupplyChainProfit() {
		iProfitService.executiveUnLineOrderSupplyChainProfit();
		iProfitService.executiveMerchantWithdrawalsIntoUnlineSupplychainProfit();
	}

	/**
	 * 
	 * @Title: toManagerProfit
	 * @Description: 跳转管理费分润页面
	 * @author WQiang
	 * @date 2017年3月27日 下午5:07:30
	 * @return
	 */
	@RequestMapping("tomanagerprofit")
	public String toManagerProfit() {
		return "/profit/unlineOrderManagerProfitList";
	}

	/**
	 * 
	 * @Title: listManagerProfits
	 * @Description: 管理费分润列表查询
	 * @author WQiang
	 * @date 2017年3月27日 下午4:16:44
	 * @param request
	 * @return
	 */
	@RequestMapping("listmanagerprofit")
	@ResponseBody
	public Result listManagerProfits(ManagerProfitRequest request) {
		return iProfitService.queryAll(request.getOrderNo(), request.getPageNum(),
				request.getPageSize());
	}

	/**
	 * 
	 * @Title: exportUnLineManagerProfit
	 * @Description: 导出管理费分润
	 * @author WQiang
	 * @date 2017年4月12日 下午4:03:42
	 * @param request
	 * @param response
	 * @param managerProfit
	 * @return
	 */
	@RequestMapping("exportunlinemanagerprofit")
	public Object exportUnLineManagerProfit(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<UnlineManagerProfitVo> list = (List<UnlineManagerProfitVo>) iProfitService.findAll("");
			if (VerifyUtil.listIsNotNull(list)) {
				List<Object[]> data = new ArrayList<Object[]>();
				for (UnlineManagerProfitVo profit : list) {
					Object[] standard = new Object[27];
					standard[0] = profit.getOrderNo();
					standard[1] = profit.getOrderMoney();
					standard[2] = profit.getChargeFee();
					standard[3] = profit.getBuyerName();
					standard[4] = profit.getSellerName();
					standard[5] = profit.getPhChargeFee();
					standard[6] = profit.getYstChargeFee();
					standard[7] = profit.getShiName();
					standard[8] = profit.getCityAgency();
					standard[9] = profit.getXianName();
					standard[10] = profit.getCountyAgency();
					standard[11] = profit.getQuName();
					standard[12] = profit.getCommunityAgency();
					standard[13] = profit.getShiRName();
					standard[14] = profit.getRecommendCity();
					standard[15] = profit.getXianRName();
					standard[16] = profit.getRecommendCounty();
					standard[17] = profit.getQuRName();
					standard[18] = profit.getRecommendCommunity();
					standard[19] = profit.getMerchantRName();
					standard[20] = profit.getRecommendMerchant();
					standard[21] = profit.getChargeTotal();
					standard[22] = profit.getChargeRemain();
					standard[23] = profit.getAuditor();
					standard[24] = profit.getAuditDate();
					standard[25] = profit.getAuditStatus();
					standard[26] = profit.getReason();
					data.add(standard);
				}

				ExcelBean excelbean = new ExcelBean();
				excelbean.setTableHeader(new String[] { "订单号", "订单金额", "管理费", "买家", "卖家", "普惠管理费",
						"易商通管理费", "市代名称", "市级代理分润", "县代名称", "县级代理分润", "社区代名称", "社区代理分润", "市代推广师名称",
						"推广师推市级代理", "县代推广师名称", "推广师推县级代理", "社区代推广师名称", "推广师推社区代理", "商户推广师名称",
						"推广师推商户", "管理费支出合计", "管理费剩余","审核人","审核日期","审核状态","审核原因" });
				excelbean.setSheetData(data);
				excelbean.setName("线下订单管理费分润表");
				ExcelUtil.download(request, response, excelbean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 修改状态 状态 审核状态 1 未审核 2 审核通过 3 审核不通过
	 * 
	 * @param merchant
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateProfitStatues", method = RequestMethod.POST)
	public Object updateProfitStatues(UnlineSupplyChainProfit unlineSupplyChainProfit) {
		return iProfitService.updateProfitStatues(unlineSupplyChainProfit);
	}
	
}
